package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.ProductPool;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.dao.ProductPoolMapper;
import com.yami.shop.service.ProductDockingService;
import com.yami.shop.service.ProductPoolService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 商品池Service实现类
 */
@Service
@Slf4j
public class ProductPoolServiceImpl extends ServiceImpl<ProductPoolMapper, ProductPool> implements ProductPoolService {

    @Resource
    private MessageSource messageSource;
    @Autowired
    private ProductDockingService productDockingService;

    @Override
    public IPage<ProductPool> pageProductPool(Page<ProductPool> page, Long shopId, String poolName) {
        return baseMapper.pageProductPool(page, shopId, poolName);
    }

    @Override
    public ProductPool getProductPoolById(Long id) {
        if (id == null) {
            throw new YamiShopBindException(messageSource.getMessage("product.pool.id.not.null", null, LocaleContextHolder.getLocale()));
        }
        return getById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveProductPool(ProductPool productPool) {
        validateProductPool(productPool);
        productPool.setCreateTime(LocalDateTime.now());
        productPool.setUpdateTime(LocalDateTime.now());
        boolean result = save(productPool);
        if (!result) {
            throw new YamiShopBindException(messageSource.getMessage("product.pool.save.failed", null, LocaleContextHolder.getLocale()));
        }
        productDockingService.dockProductsByCategory(productPool.getShopId(),productPool.getCategoryId());

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateProductPool(ProductPool productPool) {
        if (productPool.getId() == null) {
            throw new YamiShopBindException(messageSource.getMessage("product.pool.id.not.null", null, LocaleContextHolder.getLocale()));
        }
        validateProductPool(productPool);
        productPool.setUpdateTime(LocalDateTime.now());
        boolean result = updateById(productPool);
        if (!result) {
            throw new YamiShopBindException(messageSource.getMessage("product.pool.update.failed", null, LocaleContextHolder.getLocale()));
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteProductPool(Long id) {
        if (id == null) {
            throw new YamiShopBindException(messageSource.getMessage("product.pool.id.not.null", null, LocaleContextHolder.getLocale()));
        }
        boolean result = removeById(id);
        if (!result) {
            throw new YamiShopBindException(messageSource.getMessage("product.pool.delete.failed", null, LocaleContextHolder.getLocale()));
        }
    }

    /**
     * 校验商品池信息
     */
    private void validateProductPool(ProductPool productPool) {
        if (productPool.getPoolName() == null || productPool.getPoolName().trim().isEmpty()) {
            throw new YamiShopBindException(messageSource.getMessage("product.pool.name.not.null", null, LocaleContextHolder.getLocale()));
        }
        if (productPool.getCategoryId() == null) {
            throw new YamiShopBindException(messageSource.getMessage("product.pool.category.not.null", null, LocaleContextHolder.getLocale()));
        }
        if (productPool.getShopId() == null) {
            throw new YamiShopBindException(messageSource.getMessage("product.pool.shop.not.null", null, LocaleContextHolder.getLocale()));
        }
    }
} 